// pages/article-list/article-list.js
var app=getApp();
var util=require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    newslist:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var newListId=options.id;
    console.log(newListId);
    var url=app.globalData.it120Base+"/liujiahe/cms/news/list/v2?categoryId="+newListId;
    this.getNewListData(url,"newslist","");
    // 加载动画
    wx.showNavigationBarLoading({
      complete: (res) => {},
    })
  },
  getNewListData:function(url,settedKey,categoryTitle){
    var that = this;
    wx.request({
      method:'POST',
      url: url,
      success:function(res){
        that.processClassificationList(res.data,settedKey,categoryTitle);
        console.log(res.data);
      },
      fail:function(error){
        console.log(error)
      }
    })
  },
  processClassificationList:function(moviesDouban,settedKey,categoryTitle){
    var NewListData=[];
    console.log(moviesDouban);
    for (var idx in moviesDouban.data.result){
      var subject=moviesDouban.data.result[idx];
      var title=subject.title;
      if(title.length>=15){
        title=title.substring(0,15)+"...";
      }
      var classification=subject.categoryId;
      switch (classification) {
        case 59311:
          classification="国内新闻";
          break;
        case 61384:
          classification="国际新闻";
          break;
        case 61385:
          classification="军事新闻";
          break;
        case 61386:
          classification="股票";
          break;
        case 61387:
          classification="基金";
          break;
        case 61388:
          classification="科技";
          break;
        case 61389:
          classification="英超";
          break;
        case 61390:
          classification="中超";
          break;
        case 61391:
          classification="医药";
          break;
        case 61392:
          classification="历史";
          break;
        case 61393:
          classification="佛学";
          break;
        case 59310:
          classification="娱乐";
          break;
      }
      var dateAdd=subject.dateAdd.split(" ")[0];
      var temp={
        title:title,
        classification:classification,
        NewListPic:subject.pic,
        NewListViews:subject.views,
        NewListId:subject.id,
        dateAdd:dateAdd,
        views:subject.views
      }
      NewListData.push(temp);
    }

    var readyData={};
    readyData[settedKey]={
      categoryTitle:categoryTitle,
      NewListData:NewListData
    }
    this.setData(readyData);
    wx.setNavigationBarTitle({
      title:this.data.newslist.NewListData[0].classification,
    })
    wx.hideNavigationBarLoading({
      complete: (res) => {},
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //获得文章编号 带参跳转 data-new-id自定义属性 P114
  onNewlistToNewid:function(event){
    var newId=event.currentTarget.dataset.newId;
    wx.setStorageSync('NewList', wx.getStorageSync('NewList')+","+newId);
    console.log(newId);
    wx.navigateTo({
      url: '../new-id/new-id?id='+newId,
    })
  },
})